package cn.nawang.ebeim.api.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.nawang.ebeim.api.bean.DatasourceMenuBean;
import cn.nawang.ebeim.api.comm.PageList;
import cn.nawang.ebeim.api.comm.Pages;
import cn.nawang.ebeim.api.comm.SearchFormParams;
import cn.nawang.ebeim.api.domain.Customer;
import cn.nawang.ebeim.api.domain.User;
import cn.nawang.ebeim.api.service.BackupLogService;
import cn.nawang.ebeim.api.service.CustomerService;
import cn.nawang.ebeim.api.service.DatasourceService;
import cn.nawang.ebeim.api.service.HeartLogService;
import cn.nawang.ebeim.api.service.UserService;

/**
 * @ClassName: ClientFirstPageController
 * @Description: TODO
 * @author fanyb
 * @date Nov 12, 2015 6:52:36 PM
 * @version v1.0
 */
@RestController
public class ClientHomePageController {
  private static final Logger LOG = LoggerFactory.getLogger(ClientHomePageController.class);
  
  @Autowired
  private DatasourceService datasourceService;
  
  @Autowired
  private BackupLogService backupLogService;
  
  @Autowired
  private UserService userService;
  
  @Autowired
  private CustomerService customerService;
  
  @Autowired
  private HeartLogService heartLogService;
  
  @RequestMapping(value = "/token/web_client/first_page/message", method = RequestMethod.GET)
  public Map<String, Object> getHomePageInfo(String createUserId, String terminalId,String dsType) {
    LOG.info("/token/web_client/first_page/message {}", createUserId, terminalId,dsType);
    Map<String, Object> map = new HashMap<String, Object>();
    User user=userService.findOne(createUserId);
    Customer customer=customerService.findOne(user.getCustomerId());
    
    // 保险箱已使用空间大小
    map.put("safeBoxSpaceSize", datasourceService.findSafeBoxByUserId(null, createUserId).get("space_usage"));
    // 用户总空间
    map.put("userTotalSize", heartLogService.getMemoryAll(user).get("countSize"));
    // 保险箱最新更新时间
    map.put("safeBoxLastTime", backupLogService.findByDsTypeAndCreateUserId(dsType, createUserId));
    // 当前设备数
    List<DatasourceMenuBean> list = datasourceService.findBindDeviceNameByTerminalId(createUserId);
    map.put("deviceCount", list.size());
    
    // 数据源个数
    // 组装查询条件
    List<SearchFormParams> dtovect = new ArrayList<SearchFormParams>();
    // 用户id
    if (!StringUtils.isEmpty(createUserId)) {
      dtovect.add(new SearchFormParams("d.createUserId", createUserId, "createUserId",
          SearchFormParams.INTEGER_TYPE));
    }
    dsType="0";
    if (!StringUtils.isEmpty(dsType)) {
      dtovect.add(new SearchFormParams("d.dsType", dsType, "dsType",
          SearchFormParams.INTEGER_TYPE));
    }
    Pages page=new Pages();
    page.setPageSize(0);
    page.setStartPos(0);
    if (dtovect.size() > 0) {
      page.setSearchFormParams(dtovect.toArray(new SearchFormParams[dtovect.size() - 1]));
    }
    PageList pagelist = datasourceService.findAllByPaging(page);
    map.put("directoryCount", pagelist.getPage().getTotalRecord());
    LOG.debug("clientFirstPageMessage {}", map);
    return map;
  } 
}
